# -- coding: utf-8 --
# @time :
# @author : 周梦泽
# @file : .py
# @software: pycharm

"""
关键词行业词数据清洗
"""
import glob
import os
import warnings
from datetime import datetime

import send2trash

from autoTask.taobao.sycm.gjc.gjc_hyc_robot.gjc.gjc_data_clean import DataClean as GjcDataClean
from autoTask.taobao.sycm.gjc.gjc_hyc_robot.hyc.hyc_data_clean import DataClean as HycDataClean
from autoTask.taobao.sycm.utils.csv_merge import read_csv
import pandas as pd
from common.logger.log import log_
from config import data as yaml_data
from common.utils.CacheUtil import init_cache

path = yaml_data.get('download_path.path') + 'gjc_hyc_data_clean/'
current_dir = os.path.dirname(os.path.abspath(__file__))
folder_path = os.path.abspath(os.path.join(current_dir, '../../../../../../' + path))
date_time_string = datetime.today().strftime('%Y-%m-%d_%H-%M-%S')
file_name = f'gjc_hyc_data_{date_time_string}.xlsx'
gjc_csv_path = yaml_data.get('download_path.path') + 'gjc/gjc_data'
gjc_csv_folder_path = os.path.abspath(os.path.join(current_dir, '../../../../../../' + gjc_csv_path))

warnings.filterwarnings('ignore', category=DeprecationWarning)  # 忽略DeprecationWarning警告


def gjc_hyc_data(params_data: dict):
    """
    主启动方法
    1、读取关键词主题词数据
    2、读取行业词数据
    3、合并数据
    :param params_data: 包含以下参数：
    :return:
    """
    del_csv_path = glob.glob(os.path.join(folder_path, "result\\*.xlsx"))
    # print(folder_path)
    for csv_file in del_csv_path:
        # print(csv_file)
        send2trash.send2trash(csv_file)

    cache_table = init_cache(params_data)
    result = cache_table.get_search_key_df(key_type="xgfx")
    if result is None:
        # 先读取缓存，如果没有缓存再读取文件
        # print(gjc_csv_folder_path)
        total_df = read_csv(folder_path=gjc_csv_folder_path)

        # 获取全部关键词合并的结果，用于合并，减少空数据
        total_df = total_df.drop_duplicates(subset='关键词', keep='first')
        log_.info('更新缓存')
        cache_table.put_cache_by_df(key_type="xgfx", df=total_df)
    else:
        log_.info('使用缓存')
        total_df = result
    # print(total_df)
    # 数据去重  keep='first' 保留第一次出现的数据
    hyc_data_clean = HycDataClean()
    hyc_data, hyc_styler = hyc_data_clean.data_tidy(params_data)

    gjc_data_clean = GjcDataClean()
    gjc_data, gjc_styler = gjc_data_clean.data_tidy(params_data, total_df)

    merged_styler, merged_df = data_screen(total_df, hyc_data, params_data)

    # 保存文件
    csv_path = folder_path + '\\result\\{}'.format(file_name)
    with pd.ExcelWriter(csv_path) as writer:
        gjc_styler.to_excel(writer, sheet_name='行业前十延申词', index=False)
        hyc_styler.to_excel(writer, sheet_name='市场排行版词', index=False)
        merged_styler.to_excel(writer, sheet_name='市场排行版词+延申词', index=False)
    log_.info('文件路径：{}'.format(csv_path))
    return csv_path, merged_df


def data_screen(total_df, hyc_data, params):
    """
    数据筛选，在行业前十延申词筛选对应的关键词为hyc_styler添加 客单价等信息
    :param hyc_data: 整理后的行业词数据
    :param total_df: 整理后的关键词数据
    :param params:参数包含
            search_num:搜索人数比较直
            pay_rate:支付转换率比较值
            num_of_pay:支付人数比较值
    :return:
    """

    search_num = float(params.get('searchNum'))
    pay_rate = float(params.get('payRate'))
    num_of_pay = float(params.get('numOfPay'))

    total_df.reset_index(drop=True, inplace=True)
    hyc_data.reset_index(drop=True, inplace=True)
    columns_to_drop = [col for col in total_df.columns if col not in ['关键词'] and col in hyc_data.columns]
    gjc_data = total_df.drop(columns=columns_to_drop)
    # 获取列的顺序
    col_order = list(hyc_data.columns) + [col for col in gjc_data.columns if col not in hyc_data.columns]
    merged_df = pd.merge(gjc_data, hyc_data, how='right', on='关键词')[col_order].reset_index(drop=True)
    merged_df.replace('-', 0, inplace=True)
    merged_df.dropna(inplace=True)
    merged_df.iloc[:, 8:] = merged_df.iloc[:, 8:].astype('float64')
    flag = (merged_df['搜索人数'].astype(float) > search_num) & (merged_df['支付转化率'].astype(float) > pay_rate) & (
            merged_df['支付人数'].astype(float) > num_of_pay)
    styler = merged_df.style.applymap(highlight_red_font, num=search_num, subset=['搜索人数']) \
        .applymap(highlight_red_font, num=pay_rate, subset=['支付转化率']) \
        .applymap(highlight_red_font, num=num_of_pay, subset=['支付人数']) \
        .applymap(lambda x: 'color: red' if x in merged_df.loc[flag, '关键词'].tolist() else '')
    return styler, merged_df


def highlight_red_font(x, num) -> str:
    """
    当数据满足大于多少时，字体变红
    :param x:单元格，不需要传，applymap方法会自动将表格中的每个单元格传入
    :param num:比较值
    :return:
    """
    if x >= num:
        return 'color: red'
    else:
        return ''



